import plotly.io as pio
pio.renderers.default = "notebook"
from dwave.system import DWaveSampler, EmbeddingComposite
from schedule import *
import neal
import plotly.express as px
from tabu import TabuSampler
from dwave_qbsolv import QBSolv
from anneal_solver import solvelog_dwave
#Flag damit nicht unbeabsichtigt der quantumcomputer verwendet wird
use_quantum=True
#maximale Zeit
s = Schedule(time_max=5)
# maximal 3 maschinen
# bitte nur benötigte anzahl der maschinen angeben, wir haben keinen preprocessing step um unnötige maschinen heraus zufiltern
s.build_machines(2)
#Jobs
# 1:(2,3) bedeuted auf der maschine 1 wird die arbeit 2 zeiteinheiten am stück durchgeführt und 3 mal wiederholt
# es sollte wenn möglich nur die dauer auf 1 begrenzt sein, da wir keine zeit hatten die anderen optionen gut zu testen
# frühst möglicher anfangszeitpunkte können zwar gesetzt werden, dies bricht zurzeit noch einige feature und sollte deswegen nicht gemacht werden
s.create_job({0: (1, 1), 1: (1, 1)}, parallel_operations=1)
s.create_job({1: (1, 2), 0: (1, 1)}, parallel_operations=1)
s.create_job({0: (1, 1), 1: (1, 1)}, parallel_operations=1)
print(s)
---------------------------- Arbeitsplan Job 0 Operationen: Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 1 Operationen: Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 2 Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 2 Operationen: Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 ----------------------------
qubo=Qubo(s)
#start term
qubo.penalty_terms[1]=1
# nur eine maschine/job pro job/maschine gleichzeiting
qubo.penalty_terms[2]=1
qubo.penalty_terms[3]=1
# 4 hat keine bedeutung, war ursprünglich für die deadlines vorgesehen
# da wir diese aber ohne strafterme lösen, ist dieses feld leer
# Just in time
qubo.penalty_terms[5]=5
qubo.calculate_qubo()
print(f"qbits: {len(qubo.h)}")
# qubo berechnung falls mehrere Maschinen oder Jobs gleichzeitig verwendet werden sollten - (dies muss im schedule angegeben werden)
# qubo.calculate_qubo_virtuell()
qbits: 138
qubo.plot_qubo()
qubo.plot_connections_qubo_states()
qubo.plot_qubo_terms()
response = QBSolv().sample_qubo(qubo.J, num_repeats=1000)
sol3 = response.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol3)
print(temp)
qubo.plot_solution()
[[3, 9, 50, 52, 53, 94, 100], [state(j:0,m:0,t:3), state(j:0,m:1,t:4), state(j:1,m:0,t:4), state(j:1,m:1,t:1), state(j:1,m:1,t:2), state(j:2,m:0,t:2), state(j:2,m:1,t:3)], -5.800000000000001, {1: -9, 2: 0, 3: 0, 4: 0, 5: 3.2}, 9]
samples = TabuSampler().sample_qubo(qubo.J, num_reads=1000)
sol2 = samples.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol2)
print(temp)
qubo.plot_solution()
[[54, 96], [state(j:1,m:1,t:3), state(j:2,m:0,t:4)], -3.4000000000000004, {1: -4, 2: 0, 3: 0, 4: 0, 5: 0.6000000000000001}, 9]
sampler = neal.SimulatedAnnealingSampler()
sampleset = sampler.sample_qubo(qubo.J, num_reads=100)
sol1=sampleset.samples()[0]
*temp,_=qubo.interpret_solution_dict(sol1)
print(temp)
qubo.plot_solution()
[[3, 9, 50, 53, 54, 94, 98], [state(j:0,m:0,t:3), state(j:0,m:1,t:4), state(j:1,m:0,t:4), state(j:1,m:1,t:2), state(j:1,m:1,t:3), state(j:2,m:0,t:2), state(j:2,m:1,t:1)], -5.8, {1: -9, 2: 0, 3: 0, 4: 0, 5: 3.2}, 9]
if use_quantum:
answer = solvelog_dwave(qubo,samples=10000)
*temp2, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer.samples()[0].items()})
print(temp2)
qubo.plot_solution()
[[7, 95, 50, 54, 101], [state(j:0,m:1,t:2), state(j:2,m:0,t:3), state(j:1,m:0,t:4), state(j:1,m:1,t:3), state(j:2,m:1,t:4)], -5.2, {1: -7, 2: 0, 3: 0, 4: 0, 5: 1.8}, 9]
if use_quantum:
samplerq = DWaveSampler(solver=dict(qpu=True))
samplerq = EmbeddingComposite(samplerq)
init_qbits=answer.samples()[0]
reverse_schedule = [[0.0, 1.0], [10.0, 0.0], [20, 1.0]]
reverse_anneal_params = dict(anneal_schedule=reverse_schedule,
initial_state=init_qbits,
reinitialize_state=True)
reverse_response=samplerq.sample_qubo(
{x: y for x, y in qubo.J.items() if y != 0}, num_reads=1000, **reverse_anneal_params)
*temp3, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer.samples()[0].items()})
print(temp3)
qubo.plot_solution()
[[7, 95, 50, 54, 101], [state(j:0,m:1,t:2), state(j:2,m:0,t:3), state(j:1,m:0,t:4), state(j:1,m:1,t:3), state(j:2,m:1,t:4)], -5.2, {1: -7, 2: 0, 3: 0, 4: 0, 5: 1.8}, 9]
# uncomment to export it as html
!jupyter nbconvert --to=html example1.ipynb
[NbConvertApp] WARNING | Config option `kernel_spec_manager_class` not recognized by `NbConvertApp`.
[NbConvertApp] Converting notebook example1.ipynb to html
D:\Anaconda3\envs\Quantum\lib\site-packages\nbconvert\filters\datatypefilter.py:39: UserWarning: Your element with mimetype(s) dict_keys([]) is not able to be represented.
warn("Your element with mimetype(s) {mimetypes}"
[NbConvertApp] Writing 7845712 bytes to example1.html